// 测试环境设置
import { vi } from 'vitest';

// 简单的localStorage模拟
const localStorageMock = (() => {
  let store: Record<string, string> = {};
  return {
    getItem: vi.fn((key: string) => store[key] || null),
    setItem: vi.fn((key: string, value: string) => {
      store[key] = value;
    }),
    removeItem: vi.fn((key: string) => {
      delete store[key];
    }),
    clear: vi.fn(() => {
      store = {};
    }),
    get length() {
      return Object.keys(store).length;
    },
    key: vi.fn((index: number) => Object.keys(store)[index] || null),
  };
})();

Object.defineProperty(window, 'localStorage', {
  value: localStorageMock,
  writable: true,
});

// 模拟window.location
Object.defineProperty(window, 'location', {
  value: {
    href: 'http://localhost:5175',
    reload: vi.fn(),
  },
  writable: true,
});

// 设置测试环境变量
import.meta.env.VITE_API_BASE_URL = 'http://localhost:8080/api/v1';